package com.fingard.dsp.bank.directbank.cbhb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CBHB01Balance extends CBHB01Base implements IBalance {

    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            //赋值通用参数
            CBHB01Header bankHeader = new CBHB01Header();
            bankHeader.TransCode = "QRACBL";
            while (p_req7506.readRecord()) {
                bankHeader.ClientSeqno = getQryTransID();
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                xmlWriter.autoFormat = false;
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("AcctNo", p_req7506.readedDetail.actNum);
                xmlWriter.writeElementString("Ccy", convertCurToBank(p_req7506.readedDetail.actCur));
                xmlWriter.writeEndDocument();

                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                tmpRetDetail.actNum = p_req7506.readedDetail.actNum;

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CBHB/head/RespondCode");
                        String tmpRetInfo = tmpBkReader.getFirstText("/CBHB/head/RespondInfo");
                        if (tmpRetCode != null && tmpRetCode.equals("000000")) {
                            if (tmpBkReader.read("/CBHB/body")) {
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("Ccy"));
                                tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("AcctNo");
                                tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("AvailBal");
                                tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("Bal");
                            }
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = tmpRetInfo;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
